package com.woniuxy.store.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.food.client.param.UpdateFoodSpuStateParam;
import com.woniuxy.reservation.client.param.InsertReservationParam;
import com.woniuxy.reservation.client.param.SearchReservationByConditionParam;
import com.woniuxy.reservation.client.param.UpdateReservationStateParam;
import com.woniuxy.reservation.client.reservationclient.ReservationManageClient;
import com.woniuxy.reservation.client.reservationclient.ReservationQueryClient;
import com.woniuxy.reservation.client.result.ReservationClientDto;
import com.woniuxy.store.app.util.StoreIdUtil;
import com.woniuxy.store.client.ReservationClient;
import com.woniuxy.store.client.param.reservationParam.InsertReservationParams;
import com.woniuxy.store.client.param.reservationParam.SearchReservationByConditionParams;
import com.woniuxy.store.client.param.reservationParam.UpdateReservationStateParams;
import com.woniuxy.store.service.StoreInfoService;
import common.WoniuPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.Result;

import javax.validation.Valid;

@RestController
@RequestMapping("/store/reservation")
public class ReservationClientImpl implements ReservationClient {
    @Autowired
    private ReservationManageClient reservationManageClient;
    @Autowired
    private ReservationQueryClient reservationQueryClient;
    @Autowired
    private StoreInfoService storeInfoService;

    @Override
    @Log("添加预约申请")
    public Result insertReservation(@Valid InsertReservationParams param) {
        InsertReservationParam insertReservationParam = BeanUtil.toBean(param, InsertReservationParam.class);
        insertReservationParam.setStoreId(StoreIdUtil.getFromToken());
        return reservationManageClient.insertReservation(insertReservationParam);
    }

    @Override
    @Log("审核预约申请")
    public Result updateReservationState(@Valid UpdateReservationStateParams param) {
        UpdateReservationStateParam updateReservationStateParam = BeanUtil.toBean(param, UpdateReservationStateParam.class);
        updateReservationStateParam.setStoreId(StoreIdUtil.getFromToken());
        return reservationManageClient.updateReservationState(updateReservationStateParam);
    }

    @Override
    @Log("取消预约申请")
    public Result deleteReservation(Integer reservationId) {
        return reservationManageClient.deleteReservation(StoreIdUtil.getFromToken(),reservationId);
    }

    @Override
    @Log("根据条件查询预约申请")
    public Result searchReservationByCondition(@Valid SearchReservationByConditionParams var1) {
        SearchReservationByConditionParam param = BeanUtil.toBean(var1, SearchReservationByConditionParam.class);
        param.setStoreId(StoreIdUtil.getFromToken());
        Result<WoniuPage<ReservationClientDto>> result = reservationQueryClient.searchReservationByCondition(param);
        for (ReservationClientDto reservationClientDto : result.getData().getList()) {
            reservationClientDto.setStoreName(storeInfoService.getById(param.getStoreId()).getStoreName());
        }
        return result;
    }
}
